package com.zhiwu.uhf.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zhiwu.uhf.R;
import com.zhiwu.uhf.data.PublicData;
import com.zhiwu.uhf.entity.BeanInfo;

import java.util.List;
import java.util.Map;

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
    private List<BeanInfo.Data> beanList;
    private Context context;

    public RecycleAdapter(Context context, List<BeanInfo.Data> beanList) {
        this.beanList = beanList;
        this.context = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout itemOnClick;
        private TextView route_name;
        private TextView driver_name;
        private LinearLayout showDialog;
        private ImageView multiple_choice;

        private TextView bind_count;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            route_name = itemView.findViewById(R.id.route_name);
            driver_name = itemView.findViewById(R.id.driver_name);
            itemOnClick = itemView.findViewById(R.id.itemOnClick);
            showDialog = itemView.findViewById(R.id.showDialog);
            multiple_choice = itemView.findViewById(R.id.multiple_choice);
            bind_count = itemView.findViewById(R.id.bind_count);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_adapter, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.itemOnClick.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.OnItemClick(v, beanList, position);
            }
        });
        BeanInfo.Data data = beanList.get(position);
        boolean isChecked = Boolean.TRUE.equals(data.isChecked());
        holder.multiple_choice.setOnClickListener(v -> {
            if (setOnItemmultiple != null) {
                setOnItemmultiple.OnItemClick(v, beanList, position);
                data.setChecked(!isChecked);
                holder.multiple_choice.setSelected(!isChecked);
            }
        });

        holder.showDialog.setOnClickListener(v -> {
            if (setOnItemshowDialog != null) {
                setOnItemshowDialog.OnItemClick(v, beanList, position);
            }
        });
        holder.route_name.setText("线路: " + data.getRoute_name());
        holder.driver_name.setText("线路code: " + data.getRoute_code());
        if(data.getTotal() == 0) {
            holder.bind_count.setText("0");
        } else {
            Map<String, Integer> comparedContainer = PublicData.routeMap.get(data.getRoute_code());
            int sum = 0;
            if(comparedContainer != null) {
                for (int num : comparedContainer.values()) {
                    sum += num;
                }
            }
            holder.bind_count.setText(sum + "/" + data.getTotal());
            holder.bind_count.setTextColor(Color.BLUE);
            holder.bind_count.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        }



    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    private int mPosition = -1;

    public int getmPosition() {
        return mPosition;
    }

    public void setmPosition(int mPosition) {
        this.mPosition = mPosition;
    }

    private OnItemClickListener onItemClickListener;
    private OnItemClickListener setOnItemmultiple;
    private OnItemClickListener setOnItemshowDialog;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemmultiple(OnItemClickListener setOnItemmultiple) {
        this.setOnItemmultiple = setOnItemmultiple;
    }

    public void setOnItemshowDialog(OnItemClickListener setOnItemshowDialog) {
        this.setOnItemshowDialog = setOnItemshowDialog;
    }

    public interface OnItemClickListener {
        void OnItemClick(View view, List<BeanInfo.Data> data, int position);
    }

}
